package com.example.demo.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.sys.entity.Coursecheckform;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.demo.controller.Result;
import com.example.demo.sys.mapper.CoursemanagerMapper;
import com.example.demo.sys.entity.Coursemanager;

import org.springframework.web.bind.annotation.RestController;
import com.example.demo.common.tools.BaseController;

/**
 * <p>
    *  前端控制器 chrimer
    * </p>
 *
 * @author chrimer
 * @since 2020-06-28
 */
@RestController
@RequestMapping("/sys/coursemanager")
public class CoursemanagerController extends BaseController {
    @Autowired
    private CoursemanagerMapper coursemanagerMapper;

    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize) {
        Result result = new Result();
        Page<Coursemanager> page = new Page<Coursemanager>(pageNum, pageSize);
    Coursemanager parms = new Coursemanager();
        QueryWrapper<Coursemanager> warpper = new QueryWrapper<Coursemanager>(parms);

        result.setData(coursemanagerMapper.selectPage(page, warpper));
        return result;
    }

    //学生添加课程
    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(String userId,
                       String courseId,
                       String nickname) {
        Result result = new Result();
        int exit = 0 ;
        Coursemanager coursemanager = new Coursemanager();
        coursemanager.setCourseId(courseId);
        coursemanager.setUserId(userId);
        coursemanager.setNickname(nickname);

        Coursemanager coursemanagerExit = coursemanagerMapper.selectOne(new QueryWrapper<Coursemanager>(coursemanager));
        if(coursemanagerExit == null) {
            coursemanager.setGrade(0);
            coursemanagerMapper.insert(coursemanager);
        }
        else
        {
            exit = 1;
            Map<String, Object> map = new HashMap<>();
            map.put("exit", exit);
            result.setData(map);
        }
        return result;
    }

    //判断学生是否添加课程
    @RequestMapping(method= RequestMethod.POST,value="/judge")
    public Result judge(String userId,
                       String courseId) {
        Result result = new Result();
        int exit = 0 ;
        Coursemanager coursemanager = new Coursemanager();
        coursemanager.setCourseId(courseId);
        coursemanager.setUserId(userId);
        Coursemanager coursemanagerExit = coursemanagerMapper.selectOne(new QueryWrapper<Coursemanager>(coursemanager));
        if(coursemanagerExit == null) exit = 0;
        else exit = 1;

        Map<String, Object> map = new HashMap<>();
        map.put("exit", exit);
        result.setData(map);
        return result;
    }


    //查看课程评论信息总数
    @RequestMapping(method= RequestMethod.POST,value="/selectMyCourse")
    public Result selectdiscussNum(String id) {
        Result result = new Result();
        Coursemanager courseinfor = new Coursemanager();
        QueryWrapper<Coursemanager> warpper = new QueryWrapper<Coursemanager>(courseinfor);
        List<List<Coursecheckform>> myCourse = new ArrayList<>();
        List<Coursecheckform> test = null;
        List<String> courseid = null;
        courseid = coursemanagerMapper.SelectCourseid(id);
        for(int i=0;i<courseid.size();i++)
        {
            test =  coursemanagerMapper.SelectMycourse(courseid.get(i));
           
            myCourse.add(test);
            //System.out.println(myCourse);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("myCourse", myCourse);
        result.setData(map);
        return result;
    }


    //查看我的课程
    @RequestMapping(method= RequestMethod.POST,value="/selectMy")
    public Result selectMy(String id) {
        Result result = new Result();
        Coursemanager courseinfor = new Coursemanager();
        QueryWrapper<Coursemanager> warpper = new QueryWrapper<Coursemanager>(courseinfor);
        List<List<Coursecheckform>> myCourse = new ArrayList<>();
        List<Coursecheckform> test = null;
        List<String> courseid = null;
        courseid = coursemanagerMapper.SelectCourseid(id);
        for(int i=0;i<courseid.size();i++)
        {
            if(i<3) {
                test =  coursemanagerMapper.SelectMycourse(courseid.get(i));
            myCourse.add(test);
            }
            //System.out.println(myCourse);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("myCourseInfor", myCourse);
        result.setData(map);
        return result;
    }

    //老师查看当前课程下的学生
    @RequestMapping(method= RequestMethod.POST,value="/pagestudent")
    public Result pagestudent(
            Integer pageNum,
            Integer pageSize,
            String nickname,
            String courseId) {
        Result result = new Result();
        Page<Coursemanager> page = new Page<Coursemanager>(pageNum, pageSize);
        Coursemanager parms = new Coursemanager();
        QueryWrapper<Coursemanager> warpper = new QueryWrapper<Coursemanager>(parms);

        //更新学生当前总成绩
        List<String> userId = coursemanagerMapper.SelectUserid(courseId);
        for (int i =0;i<userId.size();i++)
        {
            int scoreSum = 0;
            List<Integer> score = coursemanagerMapper.SelectScore(courseId,userId.get(i));
            for(int j=0;j<score.size();j++)
            {
                scoreSum = scoreSum + score.get(j);
            }
            coursemanagerMapper.UpdateGrade(scoreSum,userId.get(i),courseId);
        }

        if(nickname != null && !"".equals(nickname)){


            warpper.nested(i-> i.eq("courseId",courseId).like("nickname",nickname));

        }
        else
            warpper.eq("courseId",courseId);
        result.setData(coursemanagerMapper.selectPage(page, warpper));
        return result;
    }


    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String id) {
        Result result = new Result();
        coursemanagerMapper.deleteById(id);
        return result;
    }

}
